package javango.forms.widgets;

import java.util.Map;

import javango.forms.widgets.TextInputWidget;
import javango.forms.widgets.Util;

/**
 * Widget class for a html TextArea..  Allows attrs of rows and cols.  Defaults to 10 rows and 80 cols
 * 
 * @author johns
 *
 */
public class TextAreaWidget extends TextInputWidget {

    public String render(String name, Object value, Map<String, Object> attrs) {
        Map<String, Object> finalAttrs = buildAttrs(attrs);
        finalAttrs.put("name", name);

        if (finalAttrs.containsKey("id")) {
                finalAttrs.put("id", String.format((String)finalAttrs.get("id"), name));
        }

        if (value != null) {
                if (value instanceof String[]){
                    String[] values = (String[])value;
                    if (values.length > 0 ) {
                            value = values[0];
                    } else {
                  	value = "";
                    }
                }
        } else {
        	value = "";
        }

        if (!finalAttrs.containsKey("rows")) finalAttrs.put("rows", "10");
        if (!finalAttrs.containsKey("cols")) finalAttrs.put("cols", "40");
        
        return String.format("<textarea%s>%s</textarea>", Util.flatatt(finalAttrs), value);
    }	
}
	